package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.realTime.EntityAuanyCreateuser;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.AbstractMap.SimpleEntry;

/**
 * Author: yuri
 * Description:  实时数据-auany渠道页面manager
 * Date: 14:32 2017/4/11
 */
@Service
public class AuanyDisplayByChannelManager extends ManagerQueryCacheBase<EntityAuanyCreateuser> {
    private static final long serialVersionUID = 1L;
    @Resource
    private BiPlatformManager biPlatformManager;
    @Resource
    private AuanyCreateuserManager auanyCreateuserManager;
    @Resource
    private AuanyTransferorderManager auanyTransferorderManager;
    @Resource
    private AuanyPayorderManager auanyPayorderManager;
    @Resource
    private AuanyUserloginManager auanyUserloginManager;
    
    @SuppressWarnings("serial")
	private static final Map<String, String> platformMap = new HashMap<String, String>()
    		{
    			{
    				put("ios","2");
    				put("android","3");
    				put("","9");
    			}
    		};

    public  Map<String, AuanyChannelData> updateDatas(Date lookDate) {
        Map<Map.Entry<String, String>, AuanyChannelData> channelDataMap = new LinkedHashMap<>();

        // 渠道新增
        List<Object[]> newAdd = auanyCreateuserManager.getAuanyNewUserByChannel(lookDate);
        if (newAdd != null && newAdd.size() > 0 && newAdd.get(0) != null) {
            for (Object[] e : newAdd) {
                String channelid = e[0].toString();
                String platform = platformMap.get(e[1].toString());
                int newUser = Integer.parseInt(e[2].toString());
                Map.Entry<String, String> key = new SimpleEntry<String, String>(channelid,platform);
                AuanyChannelData c = channelDataMap.get(key);
                if (c == null) {
                    c = new AuanyChannelData(channelid,platform);
                    channelDataMap.put(key, c);
                }
                c.setNewUser(newUser);
            }
        }

        // 渠道活跃
        List<Object[]> activeUser = auanyUserloginManager.getAuanyActiveUserByChannel(lookDate);
        if (activeUser != null && activeUser.size() > 0 && activeUser.get(0) != null) {
            for (Object[] e : activeUser) {
                String channelid = e[0].toString();
                String platform = platformMap.get(e[1].toString());
                int dau = Integer.parseInt(e[2].toString());
                Map.Entry<String, String> key = new SimpleEntry<String, String>(channelid,platform);
                AuanyChannelData c = channelDataMap.get(key);
                if (c == null) {
                	c = new AuanyChannelData(channelid,platform);
                    channelDataMap.put(key, c);
                }
                c.setActiveUser(dau);
            }
        }

        // 渠道付费帐号数
        List<Object[]> payUserNum = auanyPayorderManager.getAuanyAddCashUserByChannel(lookDate);
        if (payUserNum != null && payUserNum.size() > 0 && payUserNum.get(0) != null) {
            for (Object[] e : payUserNum) {
                String channelid = e[0].toString();
                String platform = platformMap.get(e[1].toString());
                int payNum = Integer.parseInt(e[2].toString());
                Map.Entry<String, String> key = new SimpleEntry<String, String>(channelid,platform);
                AuanyChannelData c = channelDataMap.get(key);
                if (c == null) {
                	c = new AuanyChannelData(channelid,platform);
                    channelDataMap.put(key, c);
                }
                c.setPayUser(payNum);
            }
        }

        // 渠道付费金额
        List<Object[]> payCash = auanyPayorderManager.getAuanyTotalCashByChannel(lookDate);
        if (payCash != null && payCash.size() > 0 && payCash.get(0) != null) {
            for (Object[] e : payCash) {
                String channelid = e[0].toString();
                String platform = platformMap.get(e[1].toString());
                long pay = (long) Double.parseDouble(e[2].toString());
                Map.Entry<String, String> key = new SimpleEntry<String, String>(channelid,platform);
                AuanyChannelData c = channelDataMap.get(key);
                if (c == null) {
                	c = new AuanyChannelData(channelid,platform);
                    channelDataMap.put(key, c);
                }
                c.setPay(UIHelper.getYuan(pay));
            }
        }
        
    	// notice the two lines after @see @warning
        // channelDataMap is map of line Key.Key="channelId from auany's fulluseridentity"   Key.Val="platformName from auany's devicecategory[ios/android]"
        // getChanPlatSetMap  is map of line Key.Key="channelId from bi_channel's channelid" Key.Val="platformId from bi_platform [2/3/9]"
        Map<String, AuanyChannelData> map = new LinkedHashMap<>();
        for(Map.Entry<Map.Entry<String, String>,Set<String>> entry: biPlatformManager.getChanPlatSetMap().entrySet()){
            if(null == entry.getValue() || entry.getValue().isEmpty()){
                continue;
            }
            for(String platName : entry.getValue()){
            	Map.Entry<String, String> key = new SimpleEntry<String, String>(platName,entry.getKey().getValue());
                if(!channelDataMap.containsKey(key)){
                    continue;
                }
                String channelChanId = entry.getKey().getKey();
                AuanyChannelData val = map.get(channelChanId);
                if(null == val){
                    val = channelDataMap.get(key);
                    val.setChannelid(channelChanId);
                    map.put(channelChanId,val);
                }else{
                    val.add(channelDataMap.get(key));
                }
            }
        }
        return map;
    }

    public class AuanyChannelData {
        private String channelid; // 渠道ID
        private String platformid;// 平台ID
        private int newUser; // 新增帐号
        private int activeUser; // 活跃帐号
        private int payUser; // 付费帐号
        private int pay; // 付费金额
        
        public AuanyChannelData(String channelid, String platformid){
        	this.channelid = channelid;
        	this.platformid = platformid;
        }

        public void add(AuanyChannelData val){
            if(null == val){
                return;
            }
            this.newUser += val.newUser;
            this.activeUser += val.activeUser;
            this.payUser += val.payUser;
            this.pay += val.pay;
        }
        
        public void setChannelid(String channelid){
        	this.channelid = channelid;
        }

        public String getChannelid() {
            return channelid;
        }

        public String getPlatformid() {
			return platformid;
		}

		public int getNewUser() {
            return newUser;
        }

        public void setNewUser(int newUser) {
            this.newUser = newUser;
        }

        public int getActiveUser() {
            return activeUser;
        }

        public void setActiveUser(int activeUser) {
            this.activeUser = activeUser;
        }

        public int getPayUser() {
            return payUser;
        }

        public void setPayUser(int payUser) {
            this.payUser = payUser;
        }

        public int getPay() {
            return pay;
        }

        public void setPay(int pay) {
            this.pay = pay;
        }
    }
}
